/*     */ package com.yss.sofa.cml.web.servlet;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.ObjectOutputStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;

/*     */ import javax.servlet.http.HttpServlet;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;

/*     */ 
/*     */ import com.yss.sofa.cml.Attachment;
/*     */ import com.yss.sofa.cml.Envelope;
/*     */ import com.yss.sofa.cml.extensions.FontFamily;
/*     */ import com.yss.sofa.cml.extensions.FontFamilyFactoryService;
/*     */ 
/*     */ public class FontServlet extends HttpServlet
/*     */ {
/*     */   private static final long serialVersionUID = 1L;
/*     */   private static final String CONTENT_OBJECT_TYPE = "application/octet-stream";
/*     */ 
/*     */   public void doGet(HttpServletRequest request, HttpServletResponse response)
/*     */     throws IOException
/*     */   {
/*  35 */     doPost(request, response);
/*     */   }
/*     */ 
/*     */   public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
/*  39 */     request.setCharacterEncoding("UTF-8");
/*  40 */     String action = request.getParameter("action");
/*  41 */     if ("mapping".equalsIgnoreCase(action))
/*  42 */       mapping(request, response);
/*  43 */     else if ("pkg".equalsIgnoreCase(action))
/*  44 */       pkg(request, response);
/*  45 */     else if ("list".equalsIgnoreCase(action))
/*  46 */       list(request, response);
/*     */     else
/*  48 */       check(request, response);
/*     */   }
/*     */ 
/*     */   public void mapping(HttpServletRequest request, HttpServletResponse response)
/*     */     throws IOException
/*     */   {
/*  59 */     response.setContentType("application/octet-stream");
/*  60 */     ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
/*  61 */     out.writeObject(getLocaleMapping());
/*  62 */     out.flush();
/*     */   }
/*     */ 
/*     */   public void pkg(HttpServletRequest request, HttpServletResponse response)
/*     */     throws IOException
/*     */   {
/*  72 */     String fontstr = request.getParameter("body");
/*  73 */     String[] fontNames = null;
/*  74 */     if (fontstr != null) {
/*  75 */       fontNames = fontstr.split(",");
/*     */     }
/*  77 */     response.setContentType("application/octet-stream");
/*  78 */     ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
/*  79 */     out.writeObject(getFontPackage(fontNames));
/*  80 */     out.flush();
/*     */   }
/*     */ 
/*     */   public void check(HttpServletRequest request, HttpServletResponse response)
/*     */     throws IOException
/*     */   {
/*  90 */     String c_etag = request.getHeader("If-None-Match");
/*  91 */     String s_etag = FontFamilyFactoryService.getInstance().getLastModifier();
/*     */ 
/*  93 */     if ((s_etag != null) && (!s_etag.equals(c_etag))) {
/*  94 */       response.addHeader("Etag", s_etag);
/*  95 */       response.setStatus(200);
/*     */     } else {
/*  97 */       response.setStatus(304);
/*     */     }
/*     */   }
/*     */ 
/*     */   public void list(HttpServletRequest request, HttpServletResponse response)
/*     */     throws IOException
/*     */   {
/* 108 */     response.setContentType("application/octet-stream");
/* 109 */     ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
/* 110 */     out.writeObject(getFontFamilies());
/* 111 */     out.flush();
/*     */   }
/*     */ 
/*     */   private Envelope getFontFamilies()
/*     */     throws IOException
/*     */   {
/* 120 */     Envelope envelope = new Envelope();
/* 121 */     FontFamilyFactoryService factoryService = FontFamilyFactoryService.getInstance();
/* 122 */     List<FontFamily> fontFamilies = factoryService.getAvailableFontFamlies();
/* 123 */     if (fontFamilies != null) {
/* 124 */       List list = new ArrayList();
/* 125 */       for (FontFamily fontFamily : fontFamilies) {
/* 126 */         Attachment attachment = new Attachment();
/* 127 */         ByteArrayOutputStream baos = new ByteArrayOutputStream();
/* 128 */         ObjectOutputStream out = new ObjectOutputStream(baos);
/* 129 */         out.writeObject(fontFamily);
/* 130 */         attachment.setContent(baos.toByteArray());
/* 131 */         attachment.setName(fontFamily.getName());
/* 132 */         list.add(attachment);
/*     */       }
/* 134 */       Attachment[] attachments = new Attachment[list.size()];
/* 135 */       list.toArray(attachments);
/* 136 */       envelope.setAttachments(attachments);
/*     */     }
/* 138 */     return envelope;
/*     */   }
/*     */ 
/*     */   private Envelope getFontPackage(String[] fontNames)
/*     */     throws IOException
/*     */   {
/* 147 */     FontFamilyFactoryService factoryService = FontFamilyFactoryService.getInstance();
/* 148 */     List<FontFamily> fontFamilies = factoryService.getAvailableFontFamlies();
/* 149 */     Envelope envelope = new Envelope();
/* 150 */     if (fontFamilies != null) {
/* 151 */       List list = new ArrayList();
/* 152 */       List arr = null;
/* 153 */       if (fontNames != null) {
/* 154 */         arr = Arrays.asList(fontNames);
/*     */       }
/* 156 */       for (FontFamily fontFamily : fontFamilies) {
/* 157 */         String fontName = fontFamily.getFontName();
/* 158 */         if ((arr == null) || (arr.contains(fontName))) {
/* 159 */           Attachment attachment = new Attachment();
/* 160 */           File font = factoryService.getFontFile(fontName);
/* 161 */           if (font == null) {
/* 162 */             throw new IOException(new StringBuilder().append("服务端字体库没有找到").append(fontName).append("字体文件。").toString());
/*     */           }
/* 164 */           attachment.setType(Byte.toString(fontFamily.getFontFormat()));
/* 165 */           attachment.setName(font.getName());
/* 166 */           attachment.setContent(readBytes(font));
/* 167 */           list.add(attachment);
/*     */         }
/*     */       }
/* 170 */       Attachment[] attach = new Attachment[list.size()];
/* 171 */       list.toArray(attach);
/* 172 */       envelope.setAttachments(attach);
/*     */     }
/* 174 */     return envelope;
/*     */   }
/*     */ 
/*     */   private byte[] readBytes(File ff) throws IOException {
/* 178 */     FileInputStream stream = new FileInputStream(ff);
/* 179 */     ByteArrayOutputStream baos = new ByteArrayOutputStream();
/*     */     try {
/* 181 */       int len = 65536;
/* 182 */       byte[] b = new byte[len];
/* 183 */       while (stream.read(b, 0, len) != -1)
/* 184 */         baos.write(b);
/*     */     }
/*     */     finally {
/* 187 */       stream.close();
/*     */     }
/* 189 */     return baos.toByteArray();
/*     */   }
/*     */ 
/*     */   private Envelope getLocaleMapping()
/*     */   {
/* 198 */     Envelope envelope = new Envelope();
/* 199 */     FontFamilyFactoryService factoryService = FontFamilyFactoryService.getInstance();
/* 200 */     Map mapping = factoryService.getLocaleMapping();
/* 201 */     if (mapping != null) {
/* 202 */       Iterator itr = mapping.entrySet().iterator();
/* 203 */       StringBuilder buff = new StringBuilder();
/* 204 */       while (itr.hasNext()) {
/* 205 */         Map.Entry entry = (Map.Entry)itr.next();
/* 206 */         String key = (String)entry.getKey();
/* 207 */         String value = (String)entry.getValue();
/* 208 */         buff.append(key).append("=");
/* 209 */         buff.append(value).append("\n");
/*     */       }
/* 211 */       envelope.setBody(buff.toString());
/*     */     }
/* 213 */     return envelope;
/*     */   }
/*     */ }

/* Location:           X:\repository\com\yss\sofa\cml\cml-api\1.1.0\cml-api-1.1.0.jar
 * Qualified Name:     com.yss.sofa.cml.web.servlet.FontServlet
 * JD-Core Version:    0.6.0
 */